package com.gufe.mapper.user.message;

import com.gufe.entity.MsgAndUser;
import com.gufe.vo.message.MsgListVO;
import com.gufe.vo.message.MsgUserVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface MessageMapper {

    //查询是否为好友关系
    @Select("SELECT id from msg_and_user where sender_id = #{senderId} && receiver_id = #{receiveId}")
    Long getFriendExist(Long senderId, Long receiveId);

    //插入新消息
    @Insert("INSERT INTO msgs(msg_and_user_id,message,status) value (#{msgAndUserId},#{msgContent},#{status})")
    void insertMsg(Long msgAndUserId, String msgContent,Integer status);

    //未读消息数加加
    @Update("UPDATE msg_and_user SET unread_count = unread_count+1 where id = #{msgAndUserId}")
    void addUnReadCount(Long msgAndUserId);

    //更新最后的聊天记录
    @Update("UPDATE msg_and_user SET last_msg = #{msgContent} where id = #{relationId}")
    void updateLastMsg(Long relationId, String msgContent);

    //查询两人之间的所有聊天记录
    List<MsgListVO> getMsgList(Long userId, Long chatId);

    //更新未读消息数
    @Update("UPDATE msg_and_user set unread_count = 0 where sender_id = #{userId} and receiver_id = #{chatId}")
    void cleanUnReadCount(Long userId, Long chatId);

    //查找聊天对象列表，即好友列表
    @Select("SELECT * from msg_and_user where sender_id  =#{userId}")
    List<MsgAndUser> getMsgUserList(Long userId);

    //更新用户在线状态
    @Update("UPDATE msg_and_user set receiver_online_status = #{i} where receiver_id = #{userId}")
    void setOnlineStatus(Long userId, int i);
}
